angular.module('module.login', [
        "ngCookies"
])

    .config(['$stateProvider',   '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider

                .state('login', {
                    url : '/login',
                    templateUrl : 'view/login/login.html',
                    controller : 'LoginController'
                })

        }])

    .controller('LoginController', ['$scope', '$state','$cookieStore','LoginService','RegisterService',
        function($scope,$state,$cookieStore,LoginService,RegisterService) {

        $scope.login=function(){
            $(".login-item1").addClass('activechange');
            $(".login-item2").removeClass('activechange');
            $(".login-detail").show();
            $(".login-detail0").hide();
        };
        $scope.register=function(){
            $(".login-item1").removeClass('activechange');
            $(".login-item2").addClass('activechange');
            $(".login-detail0").show();
            $(".login-detail").hide();
        };

        /*登录按钮*/
        $scope.btnLogin = function () {
            LoginService.login($scope.LoginMsg, function (data) {
                $cookieStore.put("_user",data);
                window.history.back();
            },function(message){
                alert("用户名或者密码错误");
            })
        };

        /*注册按钮*/
        $scope.btnRegist = function () {
            RegisterService.register($scope.LoginMsg, function (data) {
                data.username = $scope.LoginMsg.username;
                $cookieStore.put("_user",data);
                alert("注册成功");
                window.history.back();
                /*console.log(data);
                $scope.userInfoNew = {
                    "password": $scope.RegistMsg.password,
                    "userid": data.userid,
                    "name": $scope.RegistMsg.username
                };
                RegisterService.addInfo($scope.userInfoNew, function (data) {
                    $scope.userInfoPower = {
                        "adviser": data.userid,
                        "manager": 0,
                        "userid": data.userid,
                        "marketmanager": 0
                    };
                    RegisterService.addPower($scope.userInfoPower, function (data) {
                        $cookieStore.put("_user",data);
                        window.history.back();
                    }, function (error) {
                        alert("添加权限失败!");
                    })
                }, function (error) {
                    alert("价媒注册失败!");
                })*/
            }, function (error) {
                if(error.data){
                    alert(error.data.message);
                }else {
                    alert("注册失败")
                }
            });
        }


    }])